<?php

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$setting=$this->plugin_setting('setting');
$tabs=array(
	array('op'=>'list','title'=>'会员等级管理'),
	array('op'=>'edit','title'=>'添加/编辑等级'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $orderby='level asc';
	$total='';
	$return['list']=$this->getall('level',$params,array(),'',$orderby);
	foreach($return['list'] as &$item){
		$item['commissions']=$item['commissions']?json_decode($item['commissions'],true):array();
		$item['conditions']='默认等级';
	}
	//添加默认等级
	$default_levelset=$this->plugin_setting('default_level');
	$default_level=array(
		'id'=>'0',
		'title'=>'默认等级',
		'level'=>0,
		'conditions'=>'默认等级',
		'fee_per'=>$default_levelset['fee_per']?:0.00,
	);
	array_unshift($return['list'],$default_level);
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			array('name'=>'level','title'=>'等级权重','type'=>'string'),
			array('name'=>'type','title'=>'升级类型','type'=>'radio','options'=>'type'),
		),'action'=>array(
			array('op'=>'edit','title'=>'编辑','type'=>'href','show'=>array('level >'=>0)),
		)),
	);
	$return['htmls']=$htmls;
	$return['type']=array('自购满N元升级','付费直接开通');
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('level',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array(
			'title'=>'',
		);
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'分类设置',
		'edits'=>array(array('title'=>'基本信息','list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			array('name'=>'level','title'=>'等级权重','type'=>'string','help'=>'填写一百以内的数字，数字越大表示等级越高！不同等级的等级权重不得相等！'),
			array('name'=>'type','title'=>'升级类型','type'=>'radio','options'=>'type'),
			array('name'=>'fee_per','title'=>'自购满N元升级','type'=>'unitstring','unit'=>'元','show'=>array('type'=>0),'help'=>'购买漂流瓶费用+商城购物的费用'),
			array('name'=>'fee_open1','title'=>'开通月费','type'=>'unitstring','unit'=>'元','show'=>array('type'=>1)),
			array('name'=>'fee_open2','title'=>'开通季费','type'=>'unitstring','unit'=>'元','show'=>array('type'=>1)),
			array('name'=>'fee_open','title'=>'开通年费','type'=>'unitstring','unit'=>'元','show'=>array('type'=>1)),
			array('name'=>'pingnum','title'=>'赠送瓶子个数','type'=>'unitstring','unit'=>'个'),
			array('name'=>'cansimi','title'=>'支持看所有私密','type'=>'switch'),
			array('name'=>'siminum','title'=>'赠送看私密照人数','type'=>'unitstring','unit'=>'个'),
			array('name'=>'siminum2','title'=>'赠送看私密视频人数','type'=>'unitstring','unit'=>'个'),
		))),
	);
	$return['type']=array('自购满N元升级','付费直接开通');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'level'=>intval($postdata['level']),
		'fee_per'=>trim($postdata['fee_per']),
		'pingnum'=>intval($postdata['pingnum']),
		'siminum'=>intval($postdata['siminum']),
		'siminum2'=>intval($postdata['siminum2']),
		'cansimi'=>intval($postdata['cansimi']),
		'type'=>intval($postdata['type']),
		'fee_open'=>intval($postdata['fee_open']),
		'fee_open1'=>intval($postdata['fee_open1']),
		'fee_open2'=>intval($postdata['fee_open2']),
	);
	if($data['level']<=0 || $data['level']>100){
		exi('请填写正确的等级权重值，1-100','error');
	}
	
	$id=intval($postdata['id']);
	if($id){
		$this->update('level',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$this->insert('level',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='set_default'){
	//设置默认分佣比例
	if($_RQ['postdata']){
		$post=get_postdata();
		$this->plugin_setting('default_level',$post);
		exi('默认等级分佣比例设置成功！','','list');
	}
	$item=$this->plugin_setting('default_level');
	if(!$item){
		$item=array('fee_per'=>'0.00');
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置默认分佣比例',
		'edit'=>array(
			array('name'=>'fee_per','title'=>'自购满N元升级','type'=>'string','help'=>''),
		),
	);	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id==0){
		exi('默认等级不能删除！','error');
	}
	if($id){
		$this->delete('level',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('level',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>